/*
 * Copyright (c) 2018-2999 深圳市阿毛科技有限公司 All rights reserved.
 *
 * https://www.lamyz.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.lam.mall.api.controller;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lam.common.base.controller.BaseController;
import com.lam.common.base.domain.Result;
import com.lam.common.constant.CommonConstant;
import com.lam.common.constant.MallStateConstant;
import com.lam.common.context.TokenUtils;
import com.lam.common.exception.DataNotExistException;
import com.lam.common.utils.NumberUtil;
import com.lam.common.utils.StringUtils;
import com.lam.framework.async.ThreadUtils;
import com.lam.framework.operLog.annotation.Log;
import com.lam.mall.app.vo.ProductSimpleVo;
import com.lam.mall.app.vo.ProductVo;
import com.lam.mall.entity.ProdSku;
import com.lam.mall.entity.Product;
import com.lam.mall.entity.TransFeeTemplate;
import com.lam.mall.service.ProdSkuService;
import com.lam.mall.service.ProductService;
import com.lam.mall.service.TransFeeTemplateService;
import com.lam.mall.service.UserProdBrowseService;

import cn.hutool.core.bean.BeanUtil;

@RestController
@RequestMapping("/${frontPath}/prod")
@Log(title = "商品接口")
public class ProdApiController extends BaseController {

    @Autowired
    private ProductService prodService;
    @Autowired
    private ProdSkuService prodSkuService;
    @Autowired
    private TransFeeTemplateService transFeeTemplateService;
    @Autowired
    private UserProdBrowseService userProdBrowseService;


    /**
     * 根据分类ID获取该分类下所有的商品列表信息
     * @param categoryId
     * @return
     */
    @GetMapping("/pageProd")
    @Log(oper = "通过分类id商品列表信息")
    public Result prodList(@RequestParam(value = "categoryId") String categoryId) {
        
    	
    	
        startPage();
        List<ProductSimpleVo> list = prodService.pageByCategoryId(categoryId);
        return Result.ok(getTableData(list));
    }

    @GetMapping("/prodInfo")
    @Log(oper = "商品详情信息")
    public Result prodInfo(String prodId) {

        Product product = prodService.getSampleProdByIdCache(prodId);
        if (product == null) {
        	throw new DataNotExistException("商品不存在！");
        }

        List<ProdSku> skuList = prodSkuService.listByProdId(prodId);
        // 启用的sku列表
        List<ProdSku> useSkuList = skuList.stream().filter(sku -> NumberUtil.eq(sku.getStatus(), CommonConstant.STATUS_NORMAL_1)).collect(Collectors.toList());
        product.setSkuList(useSkuList);
        ProductVo productVo = BeanUtil.copyProperties(product, ProductVo.class);
        productVo.convertProperties(true);
        productVo.getSkuList().stream().forEach(t->t.convertProperties(true));

        // 有店铺配送的方式, 且存在运费模板，才返回运费模板的信息，供前端查阅
        if (StringUtils.equals(product.getDeliveryMode(), MallStateConstant.DELIVERY_MODE_SHOP) && product.getDeliveryTemplateId() != null) {
            TransFeeTemplate transFeeTemplate = transFeeTemplateService.selectByIdCache(product.getDeliveryTemplateId());
            productVo.setTransFeeTemplate(transFeeTemplate);
        }
        
        String userId = TokenUtils.getLoginUserid();
        if(StringUtils.isNotBlank(userId)) {
        	//有用户登录的情况下异步添加用户足迹 
            ThreadUtils.execAsync(()->userProdBrowseService.insert(product.getShopId(), prodId, userId));
        }

        return Result.ok(productVo);
    }
    
    /**
     * 通过分组标签id（tagId）获取商品列表
     */
    @GetMapping("/prodListByTagId")
    @Log(oper = "获取分组标签下的商品列表")
    public Result prodListByTagId(@RequestParam(value = "tagId") String tagId) {
    	
        startPage();
        List<ProductSimpleVo> list = prodService.pageByTagId(tagId);
        return Result.ok(getTableData(list));
    }

    @GetMapping("/lastedProdPage")
    @Log(oper = "新品推荐")
    public Result lastedProdPage() {
    	startPage();
        List<ProductSimpleVo> list = prodService.pageByPutAwayTime();
        return Result.ok(getTableData(list));
    }

    /**
     * 获取销量最多的商品列表
     * @param page
     * @return
     */
    @GetMapping("/moreBuyProdList")
    @Log(oper = "每日疯抢")
    public Result moreBuyProdList() {
    	
        startPage();
        List<ProductSimpleVo> list = prodService.moreBuyProdList();
        return Result.ok(getTableData(list));
    }

//    @GetMapping("/tagProdList")
//    @Log(oper = "首页所有标签商品接口")
//    public Result getTagProdList() {
//    	
//        return Result.ok(prodService.tagProdList());
//    }
}
